import java.util.Arrays;

public class ArraysPractice {
    public static void main(String[] args) {
        int[] array1 = new int[10];
        int[] array2 = array1;
        boolean flg = Arrays.equals(array1,array2);
        System.out.println(flg);


        int[] array3 = new int[10];
        Arrays.fill(array3,2,5,-1);
        System.out.println(Arrays.toString(array3));

    }

    //二分查找一定是建立在有序的情况下
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = (left + right) >>> 1;
            if(array[mid] < key) {
                left = mid +1;
            }else if(array[mid] > key) {
                right = mid-1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static void main10(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};
        Arrays.sort(array);//不是冒泡排序 底层快排
        //sort默认升序
        // 降序需要给比较器 目前我们知识储备不够
        //下面即为降序
        /*  Arrays.sort(array, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        */

        //System.out.println(Arrays.toString(array));
        int index1 = Arrays.binarySearch(array,13);
        System.out.println(index1);
        int index = binarySearch(array,3);
        System.out.println(index);

    }


    //拷贝的一种方式(了解)
    public static void main9(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};
        int[] array2 = array.clone();//产生一个副本 Object，产生个一模一样的。
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
        //Object
        array2[0] = 999;
        System.out.println("================");
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }

    public static void main8(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};

        int[] array2 = Arrays.copyOfRange(array,2,5);//[2,5)

        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.toString(array2));
    }


    //数组局部的拷贝
    public static void main7(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,3,array.length-3);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }


    //数组的拷贝
    public static void main6(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};
        int[] array2 = Arrays.copyOf(array,array.length);
            System.out.println(Arrays.toString(array));
            System.out.println(Arrays.toString(array2));
    }


    //数组的拷贝加扩容
    public static void main5(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};
        //扩容 2倍
        int[] array2 = Arrays.copyOf(array,2*array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }



    //数组的拷贝
    public static void main4(String[] args) {
        int[] array = {1,3,5,7,91,11,22,44,88,18,29,17,14};
        //Java定义数组
        int[] array2 = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            array2[i] = array[i];
        }
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));

        //C语言定义数组
        int a = 10;
        int[] array3 = new int[a];

    }




    //非数组的拷贝
    public static void main2(String[] args) {
        int[] array = {1,3,5,7,9};
        int[] array2 = array;
        //这个是不是拷贝？？？？ 不算拷贝 根本没有  产生 新的 内存空间

    }




    //数组转换成字符串
    public static String myToString(int[] tmp) {
        //assert tmp != null; TODO: 提示：可能需要一定的前提,需要设置一些参数(java断言很少使用)
        if(tmp == null) {
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < tmp.length; i++) {
            ret += tmp[i];
            if(i != tmp.length-1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main1(String[] args) {
        int[] array = null;
        String ret1 = Arrays.toString(array);
        String ret = myToString(array);
        System.out.println(ret);
    }
}
